package app.Disthanitian.thaistyle.EmerMez;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidClient extends Activity {
	private final String DEBUG_TAG = "AndroidClient";
	private TextView mTextView = null;
	private EditText mEditText = null;
	private Button mButton = null;
	private Socket socket = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.clientsite);

		mEditText = (EditText) findViewById(R.id.InputText);
		mButton = (Button) findViewById(R.id.btnSend);
		mTextView = (TextView) findViewById(R.id.textViewServer);
		mButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String message = mEditText.getText().toString() + "\r\n";
				try {
					socket = new Socket("192.168.2.44", 9999);
					PrintWriter out = new PrintWriter(new BufferedWriter(
							new OutputStreamWriter(socket.getOutputStream())),
							true);
					out.println(message);
					BufferedReader br = new BufferedReader(
							new InputStreamReader(socket.getInputStream()));
					String msg = br.readLine();
					if (msg != null) {
						mTextView.setText(mTextView.getText() + "\n" + msg);
					} else {
						mTextView.setText("Data Error");

					}
					out.close();
					br.close();
					socket.close();
				} catch (Exception e) {
					Log.e(DEBUG_TAG, e.toString());
				}

			}
		});
	}

}
